package com.ymatou.iapi.godmch.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.GetCsPermissionListBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.GetCsPermissionListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
/**
*获取客服权限列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCsPermissionList{
	private static GetCsPermissionListBean getcspermissionlistBean;
	private static GetCsPermissionListCall getcspermissionlistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取客服权限列表");
	}

	@Before
	public void caseUp() {
		getcspermissionlistBean=new GetCsPermissionListBean();
		getcspermissionlistCall=new GetCsPermissionListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcspermissionlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCsPermissionList_001() {
		Logger.start(true,"获取客服权限列表,成功-销售专员");
		try {
			//templateId 1-销售专员
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
			getcspermissionlistBean.setCsId(csId);
			getcspermissionlistCall.setData(getcspermissionlistBean);
			getcspermissionlistCall.callService();			
			Logger.verifyEquals("200", getcspermissionlistCall.getString("status"), "验证status");
			
			int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			JsonArray returnpermissionCodes = getcspermissionlistCall.getJsonObject().get("result").getAsJsonObject().get("permissionCodes").getAsJsonArray();
			returnpermissionCodes.forEach(rcode->{
				Logger.verifyEquals(true,listpermissionCodes.contains(rcode.getAsInt()),"验证参数permissionCodes");
			});
				
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCsPermissionList_002() {
		Logger.start(true,"获取客服权限列表,成功-发货专员");
		try {
			Integer templateId=2;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
			getcspermissionlistBean.setCsId(csId);
			getcspermissionlistCall.setData(getcspermissionlistBean);
			getcspermissionlistCall.callService();			
			Logger.verifyEquals("200", getcspermissionlistCall.getString("status"), "验证status");
			
			int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			JsonArray returnpermissionCodes = getcspermissionlistCall.getJsonObject().get("result").getAsJsonObject().get("permissionCodes").getAsJsonArray();
			returnpermissionCodes.forEach(rcode->{
				Logger.verifyEquals(true,listpermissionCodes.contains(rcode.getAsInt()),"验证参数permissionCodes");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCsPermissionList_003() {
		Logger.start(true,"获取客服权限列表,成功-普通运营");
		try {
			Integer templateId=3;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
			getcspermissionlistBean.setCsId(csId);
			getcspermissionlistCall.setData(getcspermissionlistBean);
			getcspermissionlistCall.callService();			
			Logger.verifyEquals("200", getcspermissionlistCall.getString("status"), "验证status");
			
			int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			JsonArray returnpermissionCodes = getcspermissionlistCall.getJsonObject().get("result").getAsJsonObject().get("permissionCodes").getAsJsonArray();
			returnpermissionCodes.forEach(rcode->{
				Logger.verifyEquals(true,listpermissionCodes.contains(rcode.getAsInt()),"验证参数permissionCodes");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCsPermissionList_004() {
		Logger.start(true,"获取客服权限列表,成功-高级运营");
		try {
			Integer templateId=4;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
			getcspermissionlistBean.setCsId(csId);
			getcspermissionlistCall.setData(getcspermissionlistBean);
			getcspermissionlistCall.callService();			
			Logger.verifyEquals("200", getcspermissionlistCall.getString("status"), "验证status");
			int[] permissionCodes = new DefGodMchWapper().selectPermission(templateId).stream().mapToInt(mapper->mapper.getPermissionCode()).toArray();
			List<Integer> listpermissionCodes = Arrays.stream(permissionCodes).boxed().collect(Collectors.toList());
			JsonArray returnpermissionCodes = getcspermissionlistCall.getJsonObject().get("result").getAsJsonObject().get("permissionCodes").getAsJsonArray();
			returnpermissionCodes.forEach(rcode->{
				Logger.verifyEquals(true,listpermissionCodes.contains(rcode.getAsInt()),"验证参数permissionCodes");
			});
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCsPermissionList_005() {
		Logger.start(true,"获取客服权限列表,不输入csid失败");
		try {
			getcspermissionlistCall.setData(getcspermissionlistBean);
			getcspermissionlistCall.callService();			
			Logger.verifyEquals("4001", getcspermissionlistCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCsPermissionList_006() {
		Logger.start(true,"获取客服权限列表,csid已移除没有权限");
		try {
			Integer templateId=4;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
			int csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, templateId);
	        Logger.comment("创建渠道商客服:"+csId);
	        GodmchManageCallService.RemoveCs(mchId, csId);
	        Logger.comment("移除客服:"+csId);
			getcspermissionlistBean.setCsId(csId);
			getcspermissionlistCall.setData(getcspermissionlistBean);
			getcspermissionlistCall.callService();			
			Logger.verifyEquals("5000", getcspermissionlistCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}